<?php
defined('IN_MAIN') or exit('Invalid Access!');

function email_user($in_email, $in_subject, $in_msg){
 global $vars;

 $recipient=$in_email;
 $header = "MIME-Version: 1.0\r\nContent-type:text/html;charset=$vars[charset]\r\nFrom: $vars[admin_email]";
 $encoded_subj="=?UTF-8?B?".base64_encode($in_subject)."?=";

 $email_header=email_header();
 $email_footer=email_footer();
 $in_msg=str_replace("<%email_signature%>", $vars["email_signature"], $in_msg);
 $content =$email_header.$in_msg."<br />\n<br />\n<a href='".M_URL."'>".M_URL."</a>".$email_footer;

 return mail($recipient, $encoded_subj, $content, $header);
}

function email_admin($in_subject, $in_msg, $type="s"){
 global $vars;
 $db=$vars["db"];

 //get admin email from db
 if($type=='c'){
  $sql="select * from $db->admin where re_contact='y'";
 }elseif($type=='s'){
  $sql="select * from $db->admin where re_system='y'";
 }elseif($type=='e'){
  $sql="select * from $db->admin where re_error='y'";
 }
 $t=@mysql_num_rows($r=mysql_query($sql));
 for($i=0;$i<$t;$i++){
  $admin_email.=($admin_email? "," : "").mysql_result($r, $i, "email");
 }
 if(!$admin_email){
  $admin_email=$vars["admin_email"];
 }

 $recipient=$admin_email;
 $header = "MIME-Version: 1.0\r\nContent-type:text/html;charset=$vars[charset]\r\nFrom: $vars[admin_email]\r\n";
 $encoded_subj="=?UTF-8?B?".base64_encode($in_subject)."?=\n";

 $email_header=email_header();
 $email_footer=email_footer();
 $content ="$email_header $in_msg $email_footer";
 mail($recipient, $encoded_subj, $content, $header);
}

function email_notify($in_email, $in_sub, $in_msg){
 email_user($in_email, $in_sub, $in_msg);
}

function email_header(){
global $vars;
return("
<table border='0' cellspacing='0' cellpadding='0' style='border-collapse:collapse;' align='center'>
  <tr>
    <td width='24' height='40' background='".M_URL."/images/email/frame-tl.gif'></td>
    <td width='600' height='40' background='".M_URL."/images/email/frame-t.gif'><span style='font-size:16px; font-weight:bold; background:#fff;'>".$vars['title']."</span><!--<img src='".M_URL."/images/email/logo.jpg'>--></td>
    <td width='24' height='40' background='".M_URL."/images/email/frame-tr.gif'></td>
  </tr>
  <tr>
    <td width='24' background='".M_URL."/images/email/frame-l.gif'></td>
    <td width='600' height='420' valign='top' style='font-family:arial; font-size:11px; padding-top:10px;'>");
}

function email_footer(){
return("
    </td>
    <td width='24' height='40' background='".M_URL."/images/email/frame-r.gif'></td>
  </tr>
  <tr>
    <td width='24' height='22' background='".M_URL."/images/email/frame-bl.gif'></td>
    <td width='600' height='22' background='".M_URL."/images/email/frame-b.gif'></td>
    <td width='24' height='22' background='".M_URL."/images/email/frame-br.gif'></td>
  </tr>
</table>");
}
?>